package com.tdcoding.chat.manager;

import com.tdcoding.chat.constants.ChatConstants;
import com.tdcoding.chat.constants.MainConstants;
import com.tdcoding.chat.entities.ChannelInfo;
import com.tdcoding.chat.entities.ChatInfo;
import com.tdcoding.chat.helper.Logger;
import com.tdcoding.chat.helper.ProtostuffHelper;
import com.tdcoding.chat.helper.StringHelper;

import io.netty.channel.Channel;

public class MQManager {
	private static final String TAG = "MQManager";
	private static final byte[] KEY = "chatInfos".getBytes();
	private static MQManager mqManager;

	public static MQManager getInstance() {
		if (mqManager == null) {
			mqManager = new MQManager();
		}
		return mqManager;
	}

	private MQManager() {
	}

	/**
	 * 缓存消息
	 * @param chatInfo
	 * @return
	 */
	public static long pushMsg(ChatInfo chatInfo) {
		long result = RedisManager.incrCount("chatCount");
		if (result <= -1) {
			return result;
		}
		byte[] bytes = ProtostuffHelper.serialize(chatInfo);
		String chatCount=RedisManager.getStringBykey("chatCount");
		if (!StringHelper.hasValue(chatCount)) {
			return -1;
		}
		return RedisManager.addToMap(KEY, chatCount.getBytes(), bytes);
	}
	
	/**
	 * 循环取出消息并推送
	 */
	public static void popMsg() {
		long chatCount=RedisManager.getMapCount(KEY);
		Logger.println(TAG, "popMsg", chatCount);
		for (int vari = 1; vari <=chatCount; vari++) {
			byte[]  chatBytes = RedisManager.getMapByKey(KEY, (vari+"").getBytes());
			ChatInfo chatInfo=ProtostuffHelper.deserialize(chatBytes, ChatInfo.class);
			long result =RedisManager.delMapByKey(KEY, (vari+"").getBytes());
			if (result!=-1) {
				RedisManager.decrCount("chatCount");
				pullChatInfo(chatInfo);
			}
			Logger.println(TAG, "popMsg for", chatInfo,result);
			if (vari ==chatCount) {
				popMsg();
			}
		}
	}
	
	/**
	 * 推送消息
	 * @param chatInfo
	 */
	private static void pullChatInfo(ChatInfo chatInfo) {
		Logger.println(TAG, "chattingToUser", chatInfo);
		ChannelInfo channelInfo = null;
		if (chatInfo.getChatType()==ChatConstants.CHAT_TYPE_GROUP) {
			 channelInfo =ChatManager.getChannelInfo(chatInfo.getChatToRid());
		}
		else{
			channelInfo =ChatManager.getChannelInfo(chatInfo.getChatToId());
		}
				
		if (channelInfo==null) {
			pushMsg(chatInfo);
			return ;
		}
		Channel toChannel = channelInfo.getChatChannel();
		if (toChannel != null
				&& toChannel.isActive()
				&& channelInfo.getStatus() == MainConstants.USER_STATUS_ONLINE) {// 在线
			
//			int result = DBForChat.addChatInfo(ChatConstants.MSG_SEND_SENDING,
//					chatInfo);
//			if (result==-1) {
//				pushMsg(chatInfo);
//				return ;
//			}
//			Logger.println(TAG, "chattingToUser online", result);
//			ChannelFuture channelFuture=ActionContext.writeResponse(toChannel, chatInfo.getPacketId(),
//					ChatConstants.MSG_RECEIVE,
//					ChatConstants.MSG_SEND_SENDING, chatInfo);
//			if (!channelFuture.isSuccess()) {
//				pushMsg(chatInfo);
//			}
		}
		else{
			pushMsg(chatInfo);
		}
	}
	
	public static void main(String[] args) {
//		try {
//			RedisManager.initRedisManager();
//			for (int vari = 0; vari < 100; vari++) {
//				ChatInfo chatInfo=new ChatInfo();
//				chatInfo.setChatToId(10000000+vari);
//				chatInfo.setChatName("test"+vari);
//				chatInfo.setChatDate(System.currentTimeMillis());
//				long result = pushMsg(chatInfo);
//				Logger.println(TAG, "main push",vari,result);
//			}
//			popMsg();
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//			System.out.println(e);
//		}
	}
}
